// pages/update/update.js
import {request,post} from "../../request/index.js"
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    imgs: [],
    picPaths:[],
    id:"",
    imgs: [],
    sex:"", 
   radios: [
     {
       label: '男',
       value: '男',
       checked:true
    },{
      label: '女',
      value: '女',
      checked:false
    }
   ],
   birthSelect:"请选择出生年月",
   educationList: ['高中及以下', '大专', '本科', '硕士','硕士以上'],
   educationIndex:2,
   region: ['', '', ''],
   customItem: '',
   registryList:['', '', ''],
   hightList: [
     '150cm', '151cm', '152cm', '153cm','154cm',
     '155cm', '156cm', '157cm', '158cm','159cm',
     '160cm', '161cm', '162cm', '163cm','164cm',
     '165cm', '166cm', '167cm', '168cm','169cm',
     '170cm', '171cm', '172cm', '173cm','174cm',
     '175cm', '176cm', '177cm', '178cm','179cm',
     '180cm', '181cm', '182cm', '183cm','184cm',
     '185cm', '186cm', '187cm', '188cm','189cm',
     '190cm', '191cm', '192cm', '193cm','194cm',
     '195cm', '196cm', '197cm', '198cm','199cm','200cm'
    ],
   hightIndex:2,
   housingList: ['已购房', '已购房（有贷款）', '未购房', '有能力购房','与父母同住'],
   housingIndex:2,

   incomeList: ['5千以下', '5千到1万', '1万到2万', '2万到3万','3万到5万','5万以上'],
   incomeIndex:1,

   maritalList: ['未婚', '短婚未育', '离异带孩','丧偶无孩','丧偶带孩'],
   maritalIndex:0,

   carList: ['有车', '无车'],
   carIndex:0,


   //性别
   sex:"",
   //身高
   height:"",
  //户籍
   registry:"",
  //居住地
  address:"",
  //月收入
  income:"",
  //婚姻状况
  marital:"",
  //住房
  housing:"",
  //车辆
  car:"",
  //出生年月
  birth:"",
  //职业
  profession:"",
  //联系方式
  contacts:"",
  //学历
  education:"",
  //详细描述
  details:"",
  //择偶要求
  mateRemark:""
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log('update',options.id)
      this.setData({id:options.id})
      this.getUserInfo(options.id);
      this.getUserLifePhoto(this.data.id)
  },
  getUserInfo(id) {
    request({url:app.globalData.baseUrl+"sys_member/"+id}).then(result=>{
       const user = result.data.data;
       if(user.sex == ""){
         this.setData({sex:"男"})
       }else if(user.sex == "女"){
        const radio =[
          {
            label: '男',
            value: '男',
            checked:false
         },{
           label: '女',
           value: '女',
           checked:true
         }
        ];
        this.setData({sex:"女",radios:radio})
       }

        if(user.birth != ""&&user.birth!=null){
           this.setData({birthSelect:"",birth:user.birth})
        }

        if(user.education == ""||user.education == null){
          this.setData({education:this.data.educationList[this.data.educationIndex]})
        }else{
          this.setData({education:user.education,educationIndex:this.data.educationList.indexOf(user.education)})
        }
        if(user.address == ""||user.address == null){
          
        }else{
          this.setData({address:user.address,region:user.address.split('-')})
        }
        if(user.registry == ""||user.registry == null){
          
        }else{
          this.setData({registry:user.registry,registryList:user.registry.split('-')})
        }


        if(user.height == ""||user.height == null){
          this.setData({height:this.data.hightList[this.data.hightIndex]})
        }else{
          this.setData({height:user.height,hightIndex:this.data.hightList.indexOf(user.height)})
        }


        if(user.housing == ""||user.housing == null){
          this.setData({housing:this.data.housingList[this.data.housingIndex]})
        }else{
          this.setData({housing:user.housing,housingIndex:this.data.housingList.indexOf(user.housing)})
        }

        if(user.marital == ""||user.marital == null){
          this.setData({marital:this.data.maritalList[this.data.maritalIndex]})
        }else{
          this.setData({marital:user.marital,maritalIndex:this.data.maritalList.indexOf(user.marital)})
        }

        if(user.income == ""||user.income == null){
          this.setData({income:this.data.incomeList[this.data.incomeIndex]})
        }else{
          this.setData({income:user.income,incomeIndex:this.data.incomeList.indexOf(user.income)})
        }
        if(user.car == ""||user.car == null){
          this.setData({car:this.data.carList[this.data.carIndex]})
        }else{
          this.setData({car:user.car,carIndex:this.data.carList.indexOf(user.car)})
        }
        
        if(user.profession == ""||user.profession == null){
        }else{
          this.setData({profession:user.profession})
        }

        if(user.contacts == ""||user.contacts == null){
        }else{
          this.setData({contacts:user.contacts})
        }


        if(user.details == ""||user.details == null){
        }else{
          this.setData({details:user.details})
        }
       
        if(user.mateRemark == ""||user.mateRemark == null){
        }else{
          this.setData({mateRemark:user.mateRemark})
        }
       this.setData({userInfo:user})
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
       this.getUserInfo(this.data.id)
       this.getUserLifePhoto(this.data.id)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  chooseImageTap: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#00000",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },

  // 图片本地路径
  chooseWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res.tempFilePaths[0]);
        that.upImgs(res.tempFilePaths[0], 0) //调用上传方法
      }
    }) 
  },

//上传服务器
upImgs: function (imgurl, index) {

  
  var that = this;
  wx.uploadFile({
    url: app.globalData.baseUrl+'sys_member/upload?id='+this.data.id,
    filePath: imgurl,
    name: 'file',
    header: {
      'content-type': 'multipart/form-data'
    },
    formData: null,
    success: function (res) {
      that.getUserInfo(that.data.id)
    }
  })
  console.log(imgurl)
  console.log(this.data.id)
},

radioChange: function(e) {
  this.setData({sex:e.detail.value})
},
 

bindDateChange: function(e) {
  this.setData({
    birth: e.detail.value,
    birthSelect:""
  })
},


bindPickerChange: function (e) {
  this.setData({
    educationIndex: e.detail.value,
    education:this.data.educationList[e.detail.value]
  })
},
bindHightChange: function (e) {
  this.setData({
    hightIndex: e.detail.value,
    height:this.data.hightList[e.detail.value]
  })
},
bindHousingChange: function (e) {
  this.setData({
    housingIndex: e.detail.value,
    housing:this.data.housingList[e.detail.value]
  })
},

bindIncomeChange: function (e) {
  this.setData({
    incomeIndex: e.detail.value,
    income:this.data.incomeList[e.detail.value]
  })
},

bindMaritalChange: function (e) {
  this.setData({
    maritalIndex: e.detail.value,
    marital:this.data.maritalList[e.detail.value]
  })
},
bindCarChange: function (e) {
  this.setData({
    carIndex: e.detail.value,
    car:this.data.carList[e.detail.value]
  })
},

bindRegionChange: function (e) {
  this.setData({
    region: e.detail.value,
    address: e.detail.value.join('-')
  })
},

bindRegistryChange: function (e) {
  this.setData({
    registryList: e.detail.value,
    registry:e.detail.value.join('-')
  })
},
zhiyeInput:function(e){
  this.setData({
    profession: e.detail.value
  })
},

detailsInput:function(e){
  this.setData({
    details: e.detail.value
  })
},
mateRemarkInput:function(e){
  this.setData({
    mateRemark: e.detail.value
  })
},
contactsInput:function(e){
  this.setData({
    contacts: e.detail.value
  })
},

_updateSave:function(options){
   const user = {id:this.data.id};
   //性别
   user.sex=this.data.sex;
   //身高
   user.height=this.data.height;
  //户籍
  user.registry=this.data.registry;
  //居住地
  user.address=this.data.address;
  //月收入
  user.income=this.data.income;
  //婚姻状况
  user.marital=this.data.marital;
  //住房
  user.housing=this.data.housing;
  //车辆
  user.car=this.data.car;
  //出生年月
  user.birth=this.data.birth;
  //职业
  user.profession=this.data.profession;
  //联系方式
  user.contacts=this.data.contacts;
  //学历
  user.education=this.data.education;
  //详细描述
  user.details=this.data.details;
  //择偶要求
  user.mateRemark=this.data.mateRemark;
  post({url:app.globalData.baseUrl+"sys_member/update",data:user}).then(result=>{
    const res = result.data;
    if(res.code!=0){
     }else{
       wx.switchTab({
         url: '/pages/my/index',
       })
     }
  })
},


// 上传图片
chooseImg: function (e) {
  console.log('chooseImg---',e)
  var that = this;
  var imgs = this.data.imgs;
  if (imgs.length >= 9) {
    this.setData({
      lenMore: 1
    });
    setTimeout(function () {
      that.setData({
        lenMore: 0
      });
    }, 2500);
    return false;
  }
  console.log(134567)
  wx.chooseImage({
    // count: 1, // 默认9
    sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
    success: function (res) {
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
      var tempFilePaths = res.tempFilePaths;
      let returnCode=0;
      for (var i = 0; i < tempFilePaths.length; i++) {
         returnCode=that.upLifeImgs(tempFilePaths[i],i) //调用上传方法
      }
      // console.log(imgs);
      // that.setData({
      //   imgs: imgs
      // });
    }
  });
},

getUserLifePhoto(){
  let user = {userId:this.data.id};
  post({url:app.globalData.baseUrl+"sys_member_rel_photo/list",data:user}).then(result=>{
    const res = result.data;
    if(res.code!=0){
     }else{
       this.setData({imgs:res.data})
     }
  })
},

//上传服务器
upLifeImgs: function (imgurl,i) {
  console.log('imgurl--',imgurl)
  var that = this;
  wx.uploadFile({
    url: app.globalData.baseUrl+'sys_member/upload_life?id='+this.data.id,
    filePath: imgurl,
    name: 'file',
    header: {
      'content-type': 'multipart/form-data'
    },
    formData: null,
    success: function (res) {
      console.log('upLifeImgs---',res.data);
      that.getUserLifePhoto(that.data.id)
    }
  })
  return i;
},
// 删除图片
deleteImg: function (e) {
  console.log('deleteImg--',e.currentTarget.dataset.index)
  var that = this;
  var id = e.currentTarget.dataset.index;
  post({url:app.globalData.baseUrl+"sys_member_rel_photo/delete_life_photo?id="+id}).then(result=>{
    const res = result.data;
    if(res.code!=0){
     }else{
      that.getUserLifePhoto(that.data.id);
     }
  })
  // imgs.splice(index, 1);
  // this.setData({
  //   imgs: imgs
  // });
},
// 预览图片
previewImg: function (e) {
  //获取当前图片的下标
  var index = e.currentTarget.dataset.index;
  //所有图片
  var imgs = this.data.imgs;
  let photoUrls = [];
  for(let i =0;i<imgs.length;i++){
    photoUrls.push(imgs[i].url);
  }
  wx.previewImage({
    //当前显示图片
    current: photoUrls[index],
    //所有图片
    urls: photoUrls
  })
},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
})